package com.zbl.zblproject.erp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zbl.zblproject.erp.entity.ErpOrderItem;
import com.zbl.zblproject.erp.entity.ErpTableEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/10
 */
@Repository
public interface ErpOrderItemMapper extends BaseMapper<ErpOrderItem> {

    /**
     * 查询出出入订单项中时间大于 time的orderId (没有重复)
     * @param time 时间
     * @return
     */
    List<Integer> selectGoodsIdByLastTime(LocalDateTime time);

    @Select("SELECT MIN(id) FROM zbl_erp_orderitem WHERE order_id = #{id}")
    int selectMinId(@Param("id") int id);

    @Select("SELECT order_item_id FROM zbl_erp_orderitem WHERE id = #{itemId}")
    int selectOrderItemIdById(@Param("itemId") int itemId);

    @Select("SELECT * FROM zbl_erp_orderitem WHERE order_id = #{orderId}")
    List<ErpOrderItem> selectOrderItemsByOrderId(@Param("orderId") int orderId);

    @Update("UPDATE zbl_erp_orderitem SET suppliers_id = #{suppliersId}  WHERE id = #{id} ")
    boolean updateOrderSupplierById(@Param("id") int id, @Param("suppliersId") int suppliersId);

    List<ErpOrderItem> selectOutGoods(@Param("goodsId") int goodsId);


    List<Integer> selectToBeUpdatedOrderItem(int goodsId);

    boolean updateOrderPriceByIds(@Param("price") BigDecimal price, @Param("list") List<Integer> list);



    Integer selectGoodsNumByRkAndTime(@Param("supplierId") int supplierId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);


    Integer selectGoodsNumByRkAndTimeAndUserId(@Param("supplierId") int supplierId, @Param("userId") int userId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);




    BigDecimal selectAllPriceByRkAndTime(@Param("supplierId") int supplierId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);

    BigDecimal selectAllPriceByRkAndTimeAndUserId(@Param("supplierId") int supplierId, @Param("userId") int userId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);

    ErpTableEntity selectErpTableBySupplier(@Param("supplierId") int supplierId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);

    ErpTableEntity selectErpTableByOrderSn(@Param("list") List<String> orders);

    ErpTableEntity selectUserTableEntity(@Param("supplierId") int supplierId, @Param("userId") int userId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);

    ErpTableEntity selectUserTableEntityByOrderSn(@Param("list") List<String> orderSn);

    @Delete("DELETE FROM zbl_erp_orderitem WHERE order_sn = #{orderSn}")
    boolean deleteItemsByOrderSn(@Param("orderSn") String orderSn);
}
